package com.umeox.ota.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.umeox.ota.ui.filepicker.data.FilePickerConst;
import com.umeox.ota.data.constant.Constant;
import com.abupdate.iot_libs.utils.SPFTool;
import com.abupdate.trace.Trace;

/**
 * @author fighter_lee
 * @date 2018/1/5
 */
public class UsbDiskReceiver extends BroadcastReceiver {
    private static final String TAG = "UsbDiskReceiver";
    private String path;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (null != intent.getData()) {
            path = intent.getData().getPath();
        }
        Trace.d(TAG, "onReceive() " + path);
        if (!TextUtils.isEmpty(path) && !TextUtils.equals(path, Constant.INTERNAL_STORAGE_PATH)) {
            if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
                Trace.d("usb", "unmounted");
                SPFTool.putString(FilePickerConst.KEY_USB_DISK_PATH, "");
            }
            if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
                Trace.d("usb", "mounted");
                SPFTool.putString(FilePickerConst.KEY_USB_DISK_PATH, path);
            }
        } else {
            Trace.d(TAG, "Usb disk path is null or invalid ");
        }
    }
}
